<?php

class Article extends CI_Controller
{

	function __construct()
	{
		parent::__construct();
		$this->load->helper(array('form','url'));
		 $this->load->database();
		 
	}
	
	public function index()
	{
	/*     分页代码          */
	    $num = 4;    //每页显示数量
		if($this->uri->segment(3) == FALSE)
		{
			$offset = 0;
		}else{
			if($this->uri->segment(3) ==1)
			{
				$offset=0;
			}else{
			$offset = $this->uri->segment(3)*$num-$num;   //计算篇移量
			} 
		}
		$counts = $this->db->count_all('articles');
	    $count_page = ceil($counts/$num);
        $data['count_page'] = $count_page;  
       
	    $this->db->select('id,category,title,body,tags,own,is_show,visits,created');
	    $this->db->limit($num, $offset);   
	    $this->db->order_by('id','desc');

	    $data['articles'] = $this->db->get('articles')->result_array();
	    
	    $this->load->view('manage/header');
	    $this->load->view('article/index',$data);
	    $this->load->view('manage/footer');
	}
	
	
	public function post()
	{
		
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('title','title','required');
		$this->form_validation->set_rules('body','body','required');
		if($this->form_validation->run() == FALSE)
		{
			$this->db->select('*');
			$query = $this->db->get('category');
			$cate = $query->result_array();
			$data['cate'] = $cate;
			$this->load->view('manage/header');
			$this->load->view('article/post',$data);
			$this->load->view('manage/footer');
		}else{
			$this->load->model('model_article',"",TRUE);
			$this->model_article->save();
			redirect("article/index", "refresh");
			//$this->load->view('success');
		}
	}

	public function edit()
	{
		
		$this->load->library('form_validation');
		$this->form_validation->set_rules('title','title','required');
		$this->form_validation->set_rules('body','body','required');
		
		$this->load->model('model_article');
		
		if($this->form_validation->run() == FALSE)
		{
			
		
			$query = $this->db->select('name,ename')->get('category');
			
			$cate = $query->result_array();
			
			$data['cate'] = $cate;
			
			$article = $this->model_article->get_one($this->uri->segment(4));
			
			$data['article'] = $article;
			
			$this->db->select('*')->where('ename',$article['category']);
			
			$cateis = $this->db->get('category')->result_array();
			
			$data['cateis']=$cateis[0];

			$this->load->view('manage/header');
			$this->load->view('article/edit',$data);
			$this->load->view('manage/footer');
		}else{
		   $this->model_article->update($this->input->post());
		   
		   redirect("article/index", "refresh");
		}
	}
	
	public function drop()
	{
		
		$id = $this->uri->segment(4);
		$this->db->where('id',$id)->delete('articles');
		redirect("article/index", "refresh");
	}
	
	public function batchdrop()
	{
		$post = $this->input->post('cbSelect');
		if($post)
		{
			$count = count($post);
			for($i=0;$i<$count;$i++)
			{
				$this->db->where('id',$post[$i])->delete('articles');
			}
		}
		redirect('article/index',"refresh");
	}
	
	public function batchdeactivate()
	{
		$post = $this->input->post('cbSelect');
		if($post)
		{
			$count = count($post);
			for($i=0;$i<$count;$i++)
			{
				$data = array('is_show'=>0);
				$this->db->where('id',$post[$i])->update('articles',$data);
			}
		}
		redirect('article/index',"refresh");
	}
	
public function batchactivate()
	{
		$post = $this->input->post('cbSelect');
		if($post)
		{
			$count = count($post);
			for($i=0;$i<$count;$i++)
			{
				$data = array('is_show'=>1);
				$this->db->where('id',$post[$i])->update('articles',$data);
			}
		}
		redirect('article/index',"refresh");
	}
	
	
}